<?php

/**
 * Description of votingForm
 *
 * @author Tazenda
 */
class votingForm extends sfForm
{
    public function __construct(sfGuardUser $user)
    {
        $this->user = $user;
        
        parent::__construct();
    }
    
    public function configure()
    {
        $categories = Doctrine::getTable('Category')->findAll();
        
        $categories_done = $this->user->getCategoriesDoneVoting();
        
        $wrapperForm = new sfForm();

        $i = 0;
        
        foreach($this->user->getVotes() as $vote)
        {
            $wrapperForm->embedForm($i, new VoteForm($vote));

            $i++;
        }
        
        foreach ($categories as $category) 
        {
            if(!in_array($category->getId(), $categories_done))
            {
                $vote = new Vote();
                
                $vote->setUser($this->user);
                
                $vote->setCategory($category);
                
                $wrapperForm->embedForm($i, new VoteForm($vote));
                
                $i++;
            }
        }

        $this->embedForm('votes', $wrapperForm);

        $this->widgetSchema->setNameFormat('votes_list[%s]');
        
        $this->widgetSchema->setFormFormatterName('voting');
    }

    public function save()
    {
        $values = $this->getValues();

        foreach($this->embeddedForms['votes']->getEmbeddedForms() as $key => $voteForm)
        {
            if ($voteForm instanceof VoteForm)
            {
                $formValues = isset($values['votes'][$key]) ? $values['votes'][$key] : array();

                if (!VoteForm::formValuesAreBlank($formValues) && !$voteForm->getObject()->isModified()) 
                {
                    $voteForm->updateObject($values['votes'][$key]);
                    $voteForm->getObject()->save();
                }
            }
        }
    }
    
    public function updateObjectEmbeddedForms($values, $forms = null)
    {
        if (is_array($forms))
        {
            foreach ($forms as $key => $form)
            {
                if ($form instanceof VoteForm)
                {
                    $formValues = isset($values[$key]) ? $values[$key] : array();

                    if (VoteForm::formValuesAreBlank($formValues)) 
                    {
                        unset($forms[$key]);
                    }
                }
            }
        }

        return parent::updateObjectEmbeddedForms($values, $forms);
    }

    public function saveEmbeddedForms($con = null, $forms = null)
    {
        if (is_array($forms))
        {
            foreach ($forms as $key => $form)
            {
                if ($form instanceof VoteForm)
                {
                    if (!$form->getObject()->isModified()) 
                    {
                        unset($forms[$key]);
                    }
                }
            }
        }

        return parent::saveEmbeddedForms($con, $forms);
    }
    
    public static function _formValuesAreBlank(array $fieldNames, array $values)
    {
        foreach ($fieldNames as $fieldName)
        {
            if (isset($values[$fieldName]) && !self::formValueIsBlank($values[$fieldName]))
            {
                return false;
            }
        }

        return true;
    }

    public static function formValueIsBlank($value)
    {
        if (is_array($value)){
            foreach ($value as $subValue)
            {
                if (!self::formValueIsBlank($subValue)) 
                {
                    return false;
                }
            }

            return true;
        }

        return $value ? false : true;
    }
}

?>
